﻿import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from Entities import *
#---------------------------
# Entities DonDangTuyen
#---------------------------
def Get(keyNguoiDung, DonDangTuyenID=0, DoiTuongID=0):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    results = DonDangTuyen_DoiTuong.all()
    #results = results.filter("NguoiDung", nguoidung)
    if DonDangTuyenID > 0:
            dondangtuyen = DonDangTuyen.get_by_id(DonDangTuyenID)
            results = results.filter("DonDangTuyenID", dondangtuyen)
    if DoiTuongID > 0:
        doituong = DoiTuong.get_by_id(DoiTuongID) 
        results = results.filter("DoiTuongID", doituong)
    dangtuyenDT_list = []
    for dangtuyenDT in results:
        dangtuyenDT_dict ={}
        dangtuyenDT_dict['DonDangTuyenID'] = dangtuyenDT.DonDangTuyenID.key().id()
        dangtuyenDT_dict['TenDoiTuong'] = dangtuyenDT.DoiTuongID.TenDoiTuong
        dangtuyenDT_dict['DoiTuongID'] = dangtuyenDT.DoiTuongID.key().id()
        dangtuyenDT_dict['ID'] = dangtuyenDT.key().id()			
        dangtuyenDT_list.append(dangtuyenDT_dict)
    tuple_dangtuyenDT = tuple(dangtuyenDT_list)  
    return tuple_dangtuyenDT

def GetIDDangTuyen(keyNguoiDung, DoiTuongID=0):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    results = DonDangTuyen_DoiTuong.all()
    #results = results.filter("NguoiDung", nguoidung)
    dangtuyenDT_list = []
    if DoiTuongID > 0:
        doituong = DoiTuong.get_by_id(DoiTuongID)   
        results = results.filter("DoiTuongID" , doituong)
    for dangtuyenDT in results:	
        dangtuyenDT_list.append(dangtuyenDT.DonDangTuyenID.key())
    return dangtuyenDT_list
    
def Add(keyNguoiDung,DonDangTuyenID,DoiTuongID):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    dondangtuyen = DonDangTuyen.get_by_id(int(DonDangTuyenID))
    doituong = DoiTuong.get_by_id(int(DoiTuongID)) 
    result = DonDangTuyen_DoiTuong.all().filter("DoiTuongID", doituong).filter("DonDangTuyenID", dondangtuyen)
    if result.count() == 0:                     
        dangtuyenDT = DonDangTuyen_DoiTuong(DonDangTuyenID=dondangtuyen,DoiTuongID = doituong)
        dangtuyenDT.put()         
        return "Complete"
    else:
        return "Error"

def Update(keyNguoiDung, DoiTuongID):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    nganhnghe_update = NganhNghe.get_by_id(NganhNgheID)
    linhvuc = LinhVuc.get_by_id(LinhVucID)
    nganhnghe = db.GqlQuery("SELECT * FROM NganhNghe WHERE TenNganhNghe = :1 AND LinhVucID = :2 AND NguoiDung = :3",
                              TenNganhNghe, linhvuc, nguoidung)
    if linhvuc.count > 0 and nganhnghe_update.count > 0 : 
        if nganhnghe.count() == 0:
            nganhnghe_update.TenNganhNghe = TenNganhNghe
            nganhnghe_update.LinhVucID = linhvuc
            nganhnghe_update.put()
            return "Complete"
        else:
            return "Duplicate"
    else:
        return "Error"
        
def Delete(keyNguoiDung,DonDangTuyenID = 0, DoiTuongID=0):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"	
    results = DonDangTuyen_DoiTuong.all()
    if int(DonDangTuyenID) > 0:
        dondangtuyen = DonDangTuyen.get_by_id(int(DonDangTuyenID))
        results = results.filter("DonDangTuyenID", dondangtuyen)
    if int(DoiTuongID) > 0:
        doituong = DoiTuong.get_by_id(int(DoiTuongID))
        results = results.filter("DoiTuongID", doituong)
    for dtNd in results :
        DonDangTuyen_DoiTuong.delete(dtNd)  
    return "Complete"